/**
 * Copyright &copy; 2015-2020 <a href="www.dsesa.cn/">ESA</a> All rights reserved.
 */
package com.jeeplus.modules.sys.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.entity.StockRule;
import com.jeeplus.modules.sys.service.DictValueService;
import com.jeeplus.modules.sys.service.StockRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 2019-10-08
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/stockRule")
public class StockRuleController extends BaseController {

	@Autowired
	private StockRuleService stockRuleService;

	@Autowired
	private DictValueService dictValueService;

	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/sys/stockRule/stockRuleList";
	}

	@ModelAttribute
	public StockRule get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return stockRuleService.get(id);
		}else{
			return new StockRule();
		}
	}

	@ResponseBody
	@RequestMapping(value = "getStockRule")
	public Map<String, Object> getStockRule(@RequestParam(value = "dictValueId") String dictValueId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dictValueId == null || "".equals(dictValueId)){
			map.put("rows","[]");
			map.put("total",0);
		}else{
			List<StockRule> list = stockRuleService.getStockRuleList(dictValueId);
			map.put("rows",list);
			map.put("total", list.size());
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(DictValue dictValue, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DictValue> page = dictValueService.findPage4(new Page<DictValue>(request, response), dictValue);
		return getBootstrapData(page);
	}

	@ResponseBody
	@RequestMapping(value = "save")
	public AjaxJson save(StockRule stockRule, Model model) {
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(stockRule);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		stockRuleService.save(stockRule);
		j.setSuccess(true);
		j.setMsg("保存成功！");
		return j;
	}

	@RequestMapping(value = "stockRuleForm")
	public String stockRuleForm(StockRule stockRule, String dictValueId, Model model) {
		if(StringUtils.isNotBlank(dictValueId)){//判断objectId是否为空，如果不为空的话则是新增数据，赋值前台传入的值
			stockRule.setObject(dictValueId);
		}
		model.addAttribute("stockRule", stockRule);
		return "modules/sys/stockRule/stockRuleForm";
	}

	@ResponseBody
	@RequestMapping(value = "deleteStockRule")
	public AjaxJson deleteStockRule(String id) {
		AjaxJson j = new AjaxJson();
		stockRuleService.deleteDictValue(id);
		j.setSuccess(true);
		j.setMsg("删除键值成功！");
		return j;
	}

}
